package com.doudou.zuul.filter.handler;

import com.doudou.zuul.entity.AppAccess;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class LoginVerifyHandlerChain {

    private LoginVerifyHandler header;

    public LoginVerifyHandlerChain addLastHandler(LoginVerifyHandler loginVerifyHandler) {

        Assert.notNull(header, "header of handler chain is null! ");
        Assert.notNull(loginVerifyHandler, "the handler add is null! ");

        if (header == null || loginVerifyHandler == null) {
            return this;
        }

        LoginVerifyHandler localHandler = header;
        LoginVerifyHandler nextHandler = localHandler.getNextHandler();
        //遍历到最后一个
        while (nextHandler != null) {
            localHandler = nextHandler;
            nextHandler = nextHandler.getNextHandler();
        }
        localHandler.setNextHandler(loginVerifyHandler);
        return this;
    }

    public AppAccess doInvoke(String requestUri, String appName) {
        return header.verify(requestUri, appName);
    }
}
